package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopMeasuringUnit;
import com.hyyt.shopjsp.product.service.IShopMeasuringUnitService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator on 2017/7/28.
 */
@Controller
@RequestMapping("/back/measuringUnit")
public class ShopProductMeasuringUnitController extends BaseController{
    private Logger log = Logger.getLogger(ShopProductMeasuringUnitController.class);
    private String basePath = "/back/shop/product/productMeasuringUnit/";

    @Resource
    private IShopMeasuringUnitService shopMeasuringUnitService;

    /**
     * 进入计量单位页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoMeasuringUnitPage")
    public String gotoMeasuringUnitPage(){
        return basePath + "productMeasuringUnitList";
    }

    /**
     * 查询所有计量单位
     * @param page
     * @param response
     */
    @RequestMapping(value = "listProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public void listProductMeasuringUnit(String useState, String name, Page page, HttpServletResponse response){
        Map<String , Object> param = new HashMap<>();//定义Map
        if (Utils.objectIsNotEmpty(useState)) {
            param.put("useState", useState);
        }
        if (Utils.objectIsNotEmpty(name)) {
            param.put("name", name.trim());
        }
        param.put("sortColumns","measuringUnitId desc");
        int totalRecordCount = shopMeasuringUnitService.countShopMeasuringUnit(param);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<ShopMeasuringUnit> list = shopMeasuringUnitService.queryShopMeasuringUnit(param,page.getPageRecordBeginIndex(),page.getPageSize());//参数，起始行，行尺寸
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",list);
        ResultMsg.create(param,response);//将内容响应到页面
    }

    /**
     * 根据Id获得对象
     * @param measuringUnitId
     * @return
     */
    @RequestMapping(value = "getProductMeasuringUnitObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getProductMeasuringUnitObject(Integer measuringUnitId){
        if (Utils.objectIsNotEmpty(measuringUnitId)){
            ShopMeasuringUnit shopMeasuringUnit = shopMeasuringUnitService.loadShopMeasuringUnit(measuringUnitId);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(shopMeasuringUnit));
        }
        return ResultMsg.create(false);
    }

    /**
     * 根据id进行删除对象
     * @param measuringUnitId
     * @return
     */
    @RequestMapping(value = "deleteProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteProductMeasuringUnit(Integer measuringUnitId){
        if (Utils.objectIsNotEmpty(measuringUnitId)){
            shopMeasuringUnitService.deleteShopMeasuringUnit(measuringUnitId);
        }
        return ResultMsg.create(true);
    }

    /**
     * 查询计量单位是否已存在
     * @param name
     * @return
     */
    @RequestMapping(value = "checkProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public Map checkProductMeasuringUnit(String name,Integer measuringUnitId){
        Map<String, Object> param = new HashMap<>();
        boolean flag=false;
        if (Utils.objectIsNotEmpty(name)) {
            //统计计量单位的个数
            param.put("name", name.trim());
            int countMeasuringUnitName = shopMeasuringUnitService.countShopMeasuringUnit(param);
            if (countMeasuringUnitName > 0) {
                if (Utils.objectIsNotEmpty(measuringUnitId)){
                    //统计计量单位是否为当前id的
                    param.put("measuringUnitId",measuringUnitId);
                    int countMeasuringUnitId = shopMeasuringUnitService.countShopMeasuringUnit(param);
                    if (countMeasuringUnitId==1) {
                        flag = true;
                    }
                }
            }else {
                flag=true;
            }
        }
        param.clear();
        param.put("valid",flag);//bootstrap validator 远程校验需要返回{"valid",true}
        return param;
    }

    /**
     * 保存或者修改计量单位信息
     * @param shopMeasuringUnit
     * @return
     */
    @RequestMapping(value = "saveOrUpdateProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateProductMeasuringUnit(ShopMeasuringUnit shopMeasuringUnit){
        if (Utils.objectIsNotEmpty(shopMeasuringUnit)){
            if (Utils.objectIsEmpty(shopMeasuringUnit.getMeasuringUnitId())){
                shopMeasuringUnitService.saveShopMeasuringUnit(shopMeasuringUnit);
            }else {
                shopMeasuringUnitService.updateShopMeasuringUnit(shopMeasuringUnit);
            }
        }
        return ResultMsg.create(true);
    }
}
